//
//  USChatMenuView.m
//  us
//
//  Created by karry on 2025/4/17.
//

#import "USChatMenuView.h"

@implementation USChatMenuView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setBackgroundColor:[UIColor clearColor]];
 
        UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismiss)];
        [self addGestureRecognizer:tapGR];
    }
    return self;
}

- (void)showInView:(UIView *)view withMessageModel:(EMChatMessage *)model rect:(CGRect)rect actionBlock:(void (^)(US_ChatMenuItemType))actionBlock {
    _isShow = YES;
    [self setFrame:view.bounds];
    [view addSubview:self];
    [self setActionBlcok:actionBlock];
    [self setMessageModel:model];
        
    [self becomeFirstResponder];
    
    [[UIMenuController sharedMenuController] showMenuFromView:self rect:rect];

}

- (void)dismiss {
    _isShow = NO;
    if (self.actionBlcok) {
        self.actionBlcok(US_ChatMenuItemTypeCancel);
    }
    [[UIMenuController sharedMenuController] hideMenuFromView:self];

    [self removeFromSuperview];
}

- (BOOL)canBecomeFirstResponder {
    return YES;
}

+ (NSArray<UIMenuItem *> *)getTextViewMenuItems:(EMChatMessage *)model{
    UIMenuItem *copy = [[UIMenuItem alloc] initWithTitle:@"复制" action:@selector(copyButtonDown:)];
    UIMenuItem *del  = [[UIMenuItem alloc] initWithTitle:@"删除" action:@selector(deleteButtonDown:)];
    UIMenuItem *copyAll = [[UIMenuItem alloc] initWithTitle:@"复制全部" action:@selector(copyAllButtonDown:)];
    UIMenuItem *recall = [[UIMenuItem alloc] initWithTitle:@"撤回" action:@selector(recallButtonDown:)];
    UIMenuItem *yinyong = [[UIMenuItem alloc] initWithTitle:@"引用" action:@selector(yinyongButtonDown:)];
    NSMutableArray *arr = [NSMutableArray array];
    EMMessageBodyType messageType = model.body.type;
    if (messageType == EMMessageBodyTypeText) {
        [arr addObjectsFromArray:@[copy,copyAll,yinyong,del]];
    }
   
    return arr;
}

+ (void)setChatDetMenuController{
    UIMenuItem *copy = [[UIMenuItem alloc] initWithTitle:@"复制" action:@selector(copyButtonDown:)];
    UIMenuItem *copyAll = [[UIMenuItem alloc] initWithTitle:@"复制全部" action:@selector(copyAllButtonDown:)];
    UIMenuItem *del  = [[UIMenuItem alloc] initWithTitle:@"删除" action:@selector(deleteButtonDown:)];
    UIMenuItem *save = [[UIMenuItem alloc] initWithTitle:@"保存" action:@selector(saveButtonDown:)];
    UIMenuItem *recall = [[UIMenuItem alloc] initWithTitle:@"撤回" action:@selector(recallButtonDown:)];
    UIMenuItem *reDownload = [[UIMenuItem alloc] initWithTitle:@"重新下载" action:@selector(reDownloadButtonDown:)];
    UIMenuItem *feedback = [[UIMenuItem alloc] initWithTitle:@"反馈" action:@selector(feedbackButtonDown:)];
    UIMenuItem *addEmjo = [[UIMenuItem alloc] initWithTitle:@"添加到表情" action:@selector(addExpression:)];
    UIMenuItem *yinyong = [[UIMenuItem alloc] initWithTitle:@"引用" action:@selector(yinyongButtonDown:)];
    UIMenuController *menuController = [UIMenuController sharedMenuController];
    [menuController setMenuItems:@[copy,copyAll,save,yinyong,del,recall,reDownload,feedback,addEmjo]];
}

- (void)setMessageModel:(EMChatMessage *)model {
    EMMessageBodyType messageType = model.body.type;
    UIMenuItem *copyAll = [[UIMenuItem alloc] initWithTitle:@"复制全部" action:@selector(copyAllButtonDown:)];
    UIMenuItem *del  = [[UIMenuItem alloc] initWithTitle:@"删除" action:@selector(deleteButtonDown:)];
    UIMenuItem *save = [[UIMenuItem alloc] initWithTitle:@"保存" action:@selector(saveButtonDown:)];
    UIMenuItem *recall = [[UIMenuItem alloc] initWithTitle:@"撤回" action:@selector(recallButtonDown:)];
    UIMenuItem *reDownload = [[UIMenuItem alloc] initWithTitle:@"重新下载" action:@selector(reDownloadButtonDown:)];
    UIMenuItem *feedback = [[UIMenuItem alloc] initWithTitle:@"反馈" action:@selector(feedbackButtonDown:)];
    UIMenuItem *addEmjo = [[UIMenuItem alloc] initWithTitle:@"添加到表情" action:@selector(addExpression:)];
    UIMenuItem *yinyong = [[UIMenuItem alloc] initWithTitle:@"引用" action:@selector(yinyongButtonDown:)];

    NSMutableArray *arr = [NSMutableArray array];
    if (messageType == EMMessageBodyTypeText) {
        [arr addObjectsFromArray:@[copyAll,yinyong,del]];

    } else if (messageType == EMMessageBodyTypeVoice){
        [arr addObjectsFromArray:@[yinyong,del]];
    } else if (messageType == EMMessageBodyTypeImage || messageType == EMMessageBodyTypeVideo || messageType == EMMessageBodyTypeFile) {
        [arr addObjectsFromArray:@[save, yinyong,del]];
        
     

    } else {
        [arr addObjectsFromArray:@[yinyong,del]];
    }
 
    self.menuItems = arr;
    [UIMenuController sharedMenuController].menuItems = self.menuItems;

}

+ (BOOL)canPerformAction:(SEL)action menuItems:(NSArray<UIMenuItem *> *)items{
    for (int i=0; i<items.count; i++) {
        if(items[i].action == action){
            return YES;
        }
    }
    return NO;
}

-(BOOL)canPerformAction:(SEL)action withSender:(id)sender{
    return [USChatMenuView canPerformAction:action menuItems:self.menuItems];
}

- (void)copyButtonDown:(UIMenuController *)sender {
    [self p_clickedMenuItemType:US_ChatMenuItemTypeCopy];
}
- (void)copyAllButtonDown:(UIMenuController *)sender {
    [self p_clickedMenuItemType:US_ChatMenuItemTypeCopyAll];
}
- (void)deleteButtonDown:(UIMenuController *)sender {
    [self p_clickedMenuItemType:US_ChatMenuItemTypeDelete];
}

- (void)saveButtonDown:(UIMenuController *)sender {
    [self p_clickedMenuItemType:US_ChatMenuItemTypeSave];
}

- (void)recallButtonDown:(UIMenuController *)sender {
    [self p_clickedMenuItemType:US_ChatMenuItemTypeRecall];
}

- (void)p_clickedMenuItemType:(US_ChatMenuItemType)type{
    _isShow = NO;
    [self removeFromSuperview];
    if (self.actionBlcok) {
        self.actionBlcok(type);
    }
}

- (void)reDownloadButtonDown:(UIMenuController *)sender {
    [self p_clickedMenuItemType:US_ChatMenuItemTypeReDownload];
}
- (void)feedbackButtonDown:(UIMenuController *)sender {
    [self p_clickedMenuItemType:US_ChatMenuItemTypeFeedback];
}
- (void)addExpression:(UIMenuController *)sender {
    [self p_clickedMenuItemType:US_ChatMenuItemTypeAddExpression];

}

- (void)yinyongButtonDown:(UIMenuController *)sender{
    NSLog(@"引用");
    [self p_clickedMenuItemType:US_ChatMenuItemTypeYinyong];
}

@end
